package com.engineering.elink.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.engineering.elink.service.EnrollService;

@RestController
@CrossOrigin
@RequestMapping("/api/enroll")
@Api(value="报名接口")
public class EnrollController {

	@Autowired
	private EnrollService enrollService;
	
	/**
	 * 课程报名信息
	 */
	@RequestMapping(value="/getCourseEnroll",method=RequestMethod.GET)
	@ApiOperation(value="课程报名信息")
	public Map<String,Object> getCourseEnroll(
			@ApiParam(value="课程ID",required=true) @RequestParam("courseId") Integer courseId){
		
		return enrollService.getCourseEnroll(courseId);
	}
	
	/**
	 * 报名
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ApiOperation(value = "报名")
	public Map<String, Object> add(
			@ApiParam(value = "课程id", required = true) @RequestParam("courseId") Integer courseId
			,HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		
		return enrollService.add(courseId, userId);
	}
	
	/**
	 * 取消报名
	 */
	@RequestMapping(value="/cancelCourseEnroll",method=RequestMethod.GET)
	@ApiOperation(value="取消报名")
	public Map<String,Object> cancelCourseEnroll(
			@ApiParam(value="课程ID",required=true) @RequestParam("courseId") Integer courseId,HttpServletRequest request){
		
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		
		return enrollService.cancelCourseEnroll(courseId,userId);
	}
	
}
